package cloud.maque.sso.server.service.impl;

import cloud.maque.common.core.response.R;
import cloud.maque.sms.api.dto.SmsCodeDTO;
import cloud.maque.sms.api.dto.SmsDTO;
import cloud.maque.sms.api.dto.SmsResponse;
import cloud.maque.sms.api.feign.SmsFeignClient;
import cloud.maque.sso.server.conf.MockDisabledCondition;
import cloud.maque.sso.server.conf.MockEnabledCondition;
import cloud.maque.sso.server.service.PhoneHandle;
import cn.dev33.satoken.util.SaFoxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Service;

@Service
@Conditional(MockDisabledCondition.class)
public class PhoneHandleImpl implements PhoneHandle {

    @Autowired
    SmsFeignClient smsFeignClient;

    @Override
    public String sendCode(String phone) {

        SmsCodeDTO smsDTO = new SmsCodeDTO();
        smsDTO.setPhoneNumber(phone);
        R<SmsResponse> responseR = smsFeignClient.sendCodeSms(smsDTO);
        if (responseR.getCode() != 200) {
            throw new RuntimeException("短信发送失败:" + responseR.getMsg());
        } else {
            if (!responseR.getData().isSuccess()) {
                throw new RuntimeException("短信发送失败:" + responseR.getData().getMessage());
            }
        }
        return responseR.getData().getCode();
    }
}
